package com.yanqu.road.dao.impl.activity.pixiu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pixiu.PiXiuUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class PiXiuUserDataDaoImpl extends TempDataObjectDao<PiXiuUserData> {

    public static String getPiXiuUserDataSql = "select * from `t_u_pi_xiu_data` where `activityId`=? and `userId`=?";
    public PiXiuUserData getPiXiuUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPiXiuUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updatePiXiuUserDataSql = "update `t_u_pi_xiu_data` set happyValue=?,headReward=?,buttReward=?,conditionValue=?,useTokenNum=?,headBase=?,buttBase=?,footBase=?,getReward=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(PiXiuUserData piXiuUserData) {
        piXiuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getHappyValue()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getHeadReward()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getButtReward()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getConditionValue()));
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getUseTokenNum()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getHeadBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getButtBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getFootBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getUserId()));
        if (getDb().executeNoneQuery(updatePiXiuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPiXiuUserDataSql = "insert into `t_u_pi_xiu_data`(activityId,userId,happyValue,headReward,buttReward,conditionValue,useTokenNum,headBase,buttBase,footBase,getReward) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PiXiuUserData piXiuUserData) {
        piXiuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getHappyValue()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getHeadReward()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getButtReward()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getConditionValue()));
        para.put(i++, new DbParameter(Types.BIGINT, piXiuUserData.getUseTokenNum()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getHeadBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getButtBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getFootBase()));
        para.put(i++, new DbParameter(Types.INTEGER, piXiuUserData.getGetReward()));
        if (getDb().executeNoneQuery(insertPiXiuUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PiXiuUserData parseData(ResultSet rs) throws SQLException {
        PiXiuUserData piXiuUserData = new PiXiuUserData();
        piXiuUserData.setActivityId(rs.getInt("activityId"));
        piXiuUserData.setUserId(rs.getLong("userId"));
        piXiuUserData.setHappyValue(rs.getLong("happyValue"));
        piXiuUserData.setHeadReward(rs.getInt("headReward"));
        piXiuUserData.setButtReward(rs.getInt("buttReward"));
        piXiuUserData.setConditionValue(rs.getInt("conditionValue"));
        piXiuUserData.setUseTokenNum(rs.getLong("useTokenNum"));
        piXiuUserData.setHeadBase(rs.getInt("headBase"));
        piXiuUserData.setButtBase(rs.getInt("buttBase"));
        piXiuUserData.setFootBase(rs.getInt("footBase"));
        piXiuUserData.setGetReward(rs.getInt("getReward"));

        return piXiuUserData;
    }
}
